/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Help;
import com.yuhanginfo.modules.sys.service.HelpService;

/**
 * 帮助中心Controller
 * @author lvls
 * @version 2017-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/help")
public class HelpController extends BaseController {

	@Autowired
	private HelpService helpService;
	
	@ModelAttribute
	public Help get(@RequestParam(required=false) String id) {
		Help entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = helpService.get(id);
		}
		if (entity == null){
			entity = new Help();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:help:view")
	@RequestMapping(value = {"list", ""})
	public String list(Help help, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Help> page = helpService.findPage(new Page<Help>(request, response), help); 
		model.addAttribute("page", page);
		return "modules/sys/help/helpList";
	}

	@RequiresPermissions("sys:help:edit")
	@RequestMapping(value = "form")
	public String form(Help help, Model model) {
		model.addAttribute("help", help);
		return "modules/sys/help/helpForm";
	}

	@RequiresPermissions("sys:help:edit")
	@RequestMapping(value = "save")
	public String save(Help help, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, help)){
			return form(help, model);
		}
		helpService.save(help);
		addMessage(redirectAttributes, "保存帮助中心成功");
		return "redirect:"+Global.getAdminPath()+"/sys/help/?repage";
	}
	
	@RequiresPermissions("sys:help:edit")
	@RequestMapping(value = "delete")
	public String delete(Help help, RedirectAttributes redirectAttributes) {
		helpService.delete(help);
		addMessage(redirectAttributes, "删除帮助中心成功");
		return "redirect:"+Global.getAdminPath()+"/sys/help/?repage";
	}

	@RequiresPermissions("sys:help:view")
	@RequestMapping(value = "view")
	public String view(Help help, Model model) {
		model.addAttribute("help", help);
		return "modules/sys/help/helpView";
	}
}